package com.hoolinks.sort;

import java.util.Optional;

public class OptionalInfo {

	
	/**
	 * http://blog.chinaunix.net/uid-9848525-id-4421906.html
	 * http://www.importnew.com/6675.html
	 * @param args
	 * 
	 * 
	 * http://www.open-open.com/lib/view/open1403232177575.html#nashornJavaScript  
	 */
	public static void main(String[] args) {
	
		
		 Optional<String> name = Optional.of("Sanaulla");
		 Optional empty = Optional.ofNullable(null);
		 //是否存在 isPresent
		 name.ifPresent((value) -> {
		      System.out.println("The length of the value is: " + value.length());
		    });
		//过滤下filter()
		 name.filter(x->x.contains("a")).ifPresent(x->{
			 System.out.println("filter:"+name.get());
		 });
		 //map
		 name.map(String::trim).filter(x->x.length()>1).ifPresent(x->{System.out.println("map:  "+name.get());});
		 
		 int len=name.map(String::length).orElse(0);
		 System.out.println(len);
		
		 
		 //orElse和orElseGet区别是//orElseGet与orElse方法类似，区别在于orElse传入的是默认值，
		//orElseGet可以接受一个lambda表达式生成默认值。
		System.out.println(empty.orElseGet(() -> "Default Value"));
		
		 //
	}

}
